package com.twq.patternmatch.pattern.kinds

/**
 * Created by tangweiqun on 2017/12/8.
 * 模式的种类-变量模式
 */
object Variable {
  def main(args: Array[String]): Unit = {
    //3、变量模式(Variable patterns)
    //3.1 可以将被匹配的表达式的值绑定到模式中的变量中，这样可以在expressions中使用这个变量
    val i = 1 + 1
    var result = i match {
      case 0 => "zero"
      case somethingElse => "not zero: " + somethingElse
    }
    println(result) // not zero: 2
    //3.2 编译器怎么判断是常量模式还是变量模式？
    import scala.math.{E, Pi}
    result = E match {
      case Pi => "strange math? Pi = " + Pi
      case _ => "OK"
    }
    println(result) // OK

    val pi = math.Pi
    result = E match {
      case pi => "strange math? Pi = " + pi //以小写字母开头的单词被编译器认为是变量模式
    }
    println(result) // strange math? Pi = 2.718281828459045

    result = E match {
      case `pi` => "strange math? Pi = " + pi //以反引号扩起来的为常量模式
      case _ => "OK"
    }
    println(result) //OK
  }
}
